package com.etc.service.impl;

import com.etc.dao.FlashGoodsDao;
import com.etc.dao.FlashGoodsStockDao;
import com.etc.dao.FlashSaleDao;
import com.etc.dao.GoodsPictureDao;
import com.etc.entity.FlashGoodsStock;
import com.etc.entity.FlashSale;
import com.etc.entity.po.FlashGoodsMessagePo;
import com.etc.rabbitmq.FlashMessage;
import com.etc.redis.FlashGoodsKey;
import com.etc.redis.RedisService;
import com.etc.service.FlashGoodsService;
import com.etc.service.FlashSaleService;
import com.etc.utils.MD5Util;
import com.etc.vo.FlashGoodsDetialMessageVo;
import com.etc.vo.OrderMessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 文件名:com.etc.service.impl.FlashGoodsServiceImpl
 * 描述：秒杀商品业务层
 * 作者:吴仕泉
 * 时间:2019/5/24 19:46
 */
@Service
public class FlashGoodsServiceImpl implements FlashGoodsService{

    //秒杀商品持久层接口
    @Autowired
    FlashGoodsDao flashGoodsDao;

    //redis数据缓存业务层
    @Autowired
    RedisService redisService;

    //商品图片持久层接口
    @Autowired
    GoodsPictureDao goodsPictureDao;

    //秒杀商品库存接口
    @Autowired
    FlashGoodsStockDao flashGoodsStockDao;

    //秒杀活动持久层接口
    @Autowired
    FlashSaleDao flashSaleDao;


    //根据活动ID查询参加该活动的所有商品信息
    @Override
    public List<FlashGoodsMessagePo> selectFlashSaleGoodsMessageBySaleId(Integer saleId) {
        //从redis缓存中获取参加该活动的所有商品信息
        List<FlashGoodsMessagePo> pos=null;
        pos = redisService.getList(FlashGoodsKey.getFlashGoodsList,String.valueOf(saleId));

        if(pos==null||pos.size()<=0){

            pos = flashGoodsDao.listSelectFlashGoodsBySaleId(saleId);
            if (pos!=null && pos.size()>0){
                //添加到缓存中
                redisService.setList(FlashGoodsKey.getFlashGoodsList,String.valueOf(saleId),pos);
            }
        }
        return pos;
    }

    //根据秒杀商品ID查询该商品的相关信息
    @Override
    public FlashGoodsDetialMessageVo selectFlashGoodsDetialMeaasageByFlashGoodsId(Integer fgId) {
        //从缓存池中获取该秒杀商品的库存
      Integer stack =  redisService.get(FlashGoodsKey.getFlashGoodsStock,String.valueOf(fgId),Integer.class);
        //缓存池中不存在该秒杀商品的库存数据
      if (stack == null){
           //从数据库中获取该商品的库存信息
            FlashGoodsStock fgs = flashGoodsStockDao.getFlashGoodsStockByFlashGoodsId(fgId);
            //将商品库存保存在缓存池中
         // System.out.println(fgs);
          stack = fgs.getStocknum()-fgs.getSellnum();
          //System.out.println(stack);
            redisService.set(FlashGoodsKey.getFlashGoodsStock,String.valueOf(fgId),fgs.getStocknum());
        }
        //从redis缓存池中获取该商品的详细信息
        FlashGoodsDetialMessageVo redisfgdmv = redisService.get(FlashGoodsKey.getFlashGoodsDetail,String.valueOf(fgId), FlashGoodsDetialMessageVo.class);
       //不存在该记录
        if(redisfgdmv==null){
            redisfgdmv = new FlashGoodsDetialMessageVo();
            redisfgdmv.setFlashgoodsid(fgId);
            //获取该商品的图片链接
            List<String> pictures = goodsPictureDao.listSelectPictureURLByFlashGoodsId(fgId);
            redisfgdmv.setPictureurl(pictures);
            //获取该商品的部分信息
            FlashGoodsMessagePo fgmp = flashGoodsDao.getFlashGoodsMessageByFlashGoodsId(fgId);
            redisfgdmv.setGoodsid(fgmp.getGoodsid());
            redisfgdmv.setGoodsname(fgmp.getGoodsname());
            redisfgdmv.setGoodsprice(fgmp.getGoodsprice());
            redisfgdmv.setGoodstitle(fgmp.getGoodstitle());
            redisfgdmv.setGoodsinformation(fgmp.getGoodsinformation());
            redisfgdmv.setTwolevlid(fgmp.getTwolevelid());
            redisfgdmv.setFlashgoodsprice(fgmp.getFlashgoodsprice());
            redisfgdmv.setSellnum(stack);
            redisfgdmv.setStocknum(fgmp.getStocknum());
            //获取该商品所属活动的信息
            FlashSale fs = flashSaleDao.getSelectFalshSaleByFlashGoodsId(fgId);

                redisfgdmv.setFlashsaleid(fs.getFlashsaleid());
                redisfgdmv.setFlashstarttime(fs.getFlashstarttime().getTime() + "");
                redisfgdmv.setFlashendtime(fs.getFlashendtime().getTime() + "");
            //向redis缓存池中写入该商品的详细信息
            redisService.set(FlashGoodsKey.getFlashGoodsDetail,String.valueOf(fgId),redisfgdmv);

        }
        redisfgdmv.setSellnum(stack);
        return redisfgdmv;
    }

    //根据用户的Id与用户所要秒杀的商品ID创建对应的隐藏路径
    @Override
    public String bulidPath(Integer uId, Integer fgId) {
        //从redis缓存中读取对应用户与商品的隐藏路径
        String path = redisService.get(FlashGoodsKey.getFlashGoodsPath,uId+":"+fgId,String.class);
        if(path == null){
           // System.out.println("dfasldjfalsflj");
            int num = (int)((Math.random()*1000)+999);
            String rand = MD5Util.getEncodeByMd5(num+"");
            path = rand.substring(5);
            //System.out.println("rand:"+path);
            redisService.set(FlashGoodsKey.getFlashGoodsPath,uId+":"+fgId,path);
        }
        return path;
    }

    //判断请求路径是否合法
    @Override
    public boolean judgePath(Integer uId, Integer fgId, String path) {
        if(path==null){
            return false;
        }
        String redispath = redisService.get(FlashGoodsKey.getFlashGoodsPath,uId+":"+fgId,String.class);
        if(redispath == null || !redispath.equals(path)){
            return false;
        }
       return true;
    }

    //入消息队列数据格式转化
    @Override
    public FlashMessage orderMessageVoReturnFlashMessage(OrderMessageVo omv, String address) {
       FlashMessage fm = new FlashMessage();
       fm.setUserId(omv.getUserid());
       fm.setUserAddress(address);
       fm.setUserTel(omv.getSureusertel());
       fm.setUserName(omv.getSureusername());
       fm.setFlashGoodsId(omv.getFlashgoodsid());
       fm.setFlashaslary(omv.getSurepice());
       fm.setGoodsNum(omv.getSureshopnum());
       fm.setGoodsId(omv.getGoodid());
        return fm;
    }

    //判断用户提交的订单是否在秒杀时间之内
    @Override
    public boolean judgeFlashGoodsTime(Integer fgId) {
        Long systime =  new Date().getTime();//系统当前的时间戳
        FlashSale fs = redisService.get(FlashGoodsKey.getFlashGoodsActivityTime,String.valueOf(fgId),FlashSale.class);
        if(fs == null){
            fs = flashGoodsDao.getFlashGoodsActivityBy(fgId);
            if(fs == null){
                return false;
            }
            redisService.set(FlashGoodsKey.getFlashGoodsActivityTime,String.valueOf(fgId),fs);
        }
        Long star = fs.getFlashstarttime().getTime();
        Long end = fs.getFlashendtime().getTime();
        if(systime>star && systime < end){
            return true;
        }
        return false;
    }
}
